package com.jxb.rpc;

import java.io.UnsupportedEncodingException;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.exception.RequestTimeoutException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * RPC调用端
 *
 * @author jiaobo
 * @date Created in 2024/10/25 21:37
 **/
public class RequestProducer {

    public static void main(String[] args)
            throws MQClientException, UnsupportedEncodingException, RequestTimeoutException, MQBrokerException,
            RemotingException, InterruptedException {
        String producerGroup = "rpc";
        String  topic = "RequestTopic";
        long ttl = 3000;

        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();

        Message msg = new Message(topic,
                                  "",
                                  "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
        long begin = System.currentTimeMillis();
        Message request = producer.request(msg, ttl);
        long cost = System.currentTimeMillis() - begin;
        System.out.printf("request to <%s> cost: %d replyMessage: %s %n", topic, cost, request);
        producer.shutdown();
    }

}
